package com.rapig.mail.biz;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MethodCallBIZ {

	public static String getValue(Object obj, String name) throws Exception {

		PropertyDescriptor pd = new PropertyDescriptor(name, obj.getClass());
		Method rM = pd.getReadMethod();// 获得读方法

		Object ret = rM.invoke(obj);
		if (ret == null) {
			return null;
		}

		return ret.toString();
	}

	public static void setValue(Object obj, String name, String value)
			throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor(name, obj.getClass());
		Method wM = pd.getWriteMethod();
		wM.invoke(obj, value);
	}

	public static String codeBean(Class clazz) {
		StringBuffer code = new StringBuffer();

		String clzName = clazz.getName();
		clzName = clzName.substring(clzName.lastIndexOf(".") + 1);
		for (Field f : clazz.getDeclaredFields()) {
			String name = f.getName();
			if (!name.matches("[a-z][^_]*")) {
				continue;
			}

			code.append("\r\nprivate static String n_" + name + " = \"" + name
					+ "\";");
		}

		code.append("\r\n\r\n\r\n");
		// toJsonObject
		code.append("\r\npublic static JSONObject toJsonObject(" + clzName
				+ " obj) {");
		code.append("\r\n\ttry {");
		code.append("\r\n\t\tJSONObject jObj = new JSONObject();");
		for (Field f : clazz.getDeclaredFields()) {
			String name = f.getName();
			if (!name.matches("[a-z][^_]*")) {
				continue;
			}
			String type = f.getType().toString();
			type = type.substring(type.lastIndexOf(".") + 1);
			if ("Date".equals(type)) {
				code.append("\r\n\t\tjObj.put(n_" + name + ", obj.get"
						+ upperFirst(name) + "().getTime());");
			} else {
				code.append("\r\n\t\tjObj.put(n_" + name + ", obj.get"
						+ upperFirst(name) + "());");
			}
		}
		code.append("\r\n\t\treturn jObj;");
		code.append("\r\n\t} catch (Exception ex) {");
		code.append("\r\n\t}");
		code.append("\r\n\treturn null;");
		code.append("\r\n}");

		code.append("\r\n\r\n");
		// toJsonList
		code.append("\r\npublic static String toJsonList(List<" + clzName
				+ "> list) {");
		code.append("\r\n\tJSONArray array = new JSONArray();");
		code.append("\r\n\tfor (" + clzName + " obj : list) {");
		code.append("\r\n\t\tarray.put(toJsonObject(obj));");
		code.append("\r\n\t}");
		code.append("\r\n\treturn array.toString();");
		code.append("\r\n}");

		code.append("\r\n\r\n");
		// toJson
		code.append("\r\npublic static String toJson(" + clzName + " obj) {");
		code.append("\r\n\tJSONObject jObj = toJsonObject(obj);");
		code.append("\r\n\tif (jObj != null) {");
		code.append("\r\n\t\treturn jObj.toString();");
		code.append("\r\n\t}");
		code.append("\r\n\treturn null;");
		code.append("\r\n}");

		code.append("\r\n\r\n");
		// fromJsonObject
		code.append("\r\npublic static " + clzName
				+ " fromJsonObject(JSONObject jObj) {");
		code.append("\r\n\t" + clzName + " obj = null;");
		code.append("\r\n\ttry {");
		code.append("\r\n\t\tobj = new " + clzName + "();");
		for (Field f : clazz.getDeclaredFields()) {
			String name = f.getName();
			if (!name.matches("[a-z][^_]*")) {
				continue;
			}
			code.append("\r\n\t\tif (!jObj.isNull(n_" + name + ")) {");
			String type = f.getType().toString();
			type = type.substring(type.lastIndexOf(".") + 1);
			if ("Integer".equals(type)) {
				type = "Int";
			}
			if ("Date".equals(type)) {
				code.append("\r\n\t\t\tobj.set" + upperFirst(name)
						+ "(new Date(jObj.getLong(n_" + name + ")));");
			} else {
				code.append("\r\n\t\t\tobj.set" + upperFirst(name)
						+ "(jObj.get" + type + "(n_" + name + "));");
			}
			code.append("\r\n\t\t}");
		}
		code.append("\r\n\t} catch (Exception ex) {");
		code.append("\r\n\t}");
		code.append("\r\n\treturn obj;");
		code.append("\r\n}");

		code.append("\r\n\r\n");
		// fromJsonList
		code.append("\r\npublic static List<" + clzName
				+ "> fromJsonList(String json) {");
		code.append("\r\n\tList<" + clzName + "> list = new ArrayList<"
				+ clzName + ">();");
		code.append("\r\n\ttry {");
		code.append("\r\n\t\tJSONArray array = new JSONArray(json);");
		code.append("\r\n\t\tfor (int i = 0; i < array.length(); i++) {");
		code.append("\r\n\t\t\tJSONObject jObj = array.getJSONObject(i);");
		code.append("\r\n\t\t\tif (jObj != null) {");
		code.append("\r\n\t\t\t\tlist.add(fromJsonObject(jObj));");
		code.append("\r\n\t\t\t}");
		code.append("\r\n\t\t}");
		code.append("\r\n\t} catch (Exception ex) {");
		code.append("\r\n\t}");
		code.append("\r\n\treturn list;");
		code.append("\r\n}");

		code.append("\r\n\r\n");
		// fromJson
		code.append("\r\npublic static " + clzName + " fromJson(String json) {");
		code.append("\r\n\ttry {");
		code.append("\r\n\t\tJSONObject jObj = new JSONObject(json);");
		code.append("\r\n\t\treturn fromJsonObject(jObj);");
		code.append("\r\n\t} catch (Exception ex) {");
		code.append("\r\n\t}");
		code.append("\r\n\treturn null;");
		code.append("\r\n}");

		return code.toString();
	}

	public static String codeDAO(Class clazz) {
		return null;
	}

	private static String upperFirst(String name) {
		String first = name.substring(0, 1);
		String others = name.substring(1);

		return first.toUpperCase() + others;
	}

	public static String toJson(Object obj, String name) throws Exception {

		PropertyDescriptor pd = new PropertyDescriptor(name, obj.getClass());
		Method rM = pd.getReadMethod();// 获得读方法

		Object ret = rM.invoke(obj);
		if (ret == null) {
			return null;
		}

		return ret.toString();
	}
}
